/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entityprojet;

import entityprojet.exceptions.IllegalOrphanException;
import entityprojet.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author T.Ron
 */
public class ExercicecomptaJpaController implements Serializable {

    public ExercicecomptaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("projetT_RPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Exercicecompta exercicecompta) {
        if (exercicecompta.getPeriodeCollection() == null) {
            exercicecompta.setPeriodeCollection(new ArrayList<Periode>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Periode> attachedPeriodeCollection = new ArrayList<Periode>();
            for (Periode periodeCollectionPeriodeToAttach : exercicecompta.getPeriodeCollection()) {
                periodeCollectionPeriodeToAttach = em.getReference(periodeCollectionPeriodeToAttach.getClass(), periodeCollectionPeriodeToAttach.getPeriodePK());
                attachedPeriodeCollection.add(periodeCollectionPeriodeToAttach);
            }
            exercicecompta.setPeriodeCollection(attachedPeriodeCollection);
            em.persist(exercicecompta);
            for (Periode periodeCollectionPeriode : exercicecompta.getPeriodeCollection()) {
                Exercicecompta oldExercicecomptaOfPeriodeCollectionPeriode = periodeCollectionPeriode.getExercicecompta();
                periodeCollectionPeriode.setExercicecompta(exercicecompta);
                periodeCollectionPeriode = em.merge(periodeCollectionPeriode);
                if (oldExercicecomptaOfPeriodeCollectionPeriode != null) {
                    oldExercicecomptaOfPeriodeCollectionPeriode.getPeriodeCollection().remove(periodeCollectionPeriode);
                    oldExercicecomptaOfPeriodeCollectionPeriode = em.merge(oldExercicecomptaOfPeriodeCollectionPeriode);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Exercicecompta exercicecompta) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Exercicecompta persistentExercicecompta = em.find(Exercicecompta.class, exercicecompta.getNumexo());
            Collection<Periode> periodeCollectionOld = persistentExercicecompta.getPeriodeCollection();
            Collection<Periode> periodeCollectionNew = exercicecompta.getPeriodeCollection();
            List<String> illegalOrphanMessages = null;
            for (Periode periodeCollectionOldPeriode : periodeCollectionOld) {
                if (!periodeCollectionNew.contains(periodeCollectionOldPeriode)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Periode " + periodeCollectionOldPeriode + " since its exercicecompta field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Periode> attachedPeriodeCollectionNew = new ArrayList<Periode>();
            for (Periode periodeCollectionNewPeriodeToAttach : periodeCollectionNew) {
                periodeCollectionNewPeriodeToAttach = em.getReference(periodeCollectionNewPeriodeToAttach.getClass(), periodeCollectionNewPeriodeToAttach.getPeriodePK());
                attachedPeriodeCollectionNew.add(periodeCollectionNewPeriodeToAttach);
            }
            periodeCollectionNew = attachedPeriodeCollectionNew;
            exercicecompta.setPeriodeCollection(periodeCollectionNew);
            exercicecompta = em.merge(exercicecompta);
            for (Periode periodeCollectionNewPeriode : periodeCollectionNew) {
                if (!periodeCollectionOld.contains(periodeCollectionNewPeriode)) {
                    Exercicecompta oldExercicecomptaOfPeriodeCollectionNewPeriode = periodeCollectionNewPeriode.getExercicecompta();
                    periodeCollectionNewPeriode.setExercicecompta(exercicecompta);
                    periodeCollectionNewPeriode = em.merge(periodeCollectionNewPeriode);
                    if (oldExercicecomptaOfPeriodeCollectionNewPeriode != null && !oldExercicecomptaOfPeriodeCollectionNewPeriode.equals(exercicecompta)) {
                        oldExercicecomptaOfPeriodeCollectionNewPeriode.getPeriodeCollection().remove(periodeCollectionNewPeriode);
                        oldExercicecomptaOfPeriodeCollectionNewPeriode = em.merge(oldExercicecomptaOfPeriodeCollectionNewPeriode);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = exercicecompta.getNumexo();
                if (findExercicecompta(id) == null) {
                    throw new NonexistentEntityException("The exercicecompta with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Exercicecompta exercicecompta;
            try {
                exercicecompta = em.getReference(Exercicecompta.class, id);
                exercicecompta.getNumexo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The exercicecompta with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Periode> periodeCollectionOrphanCheck = exercicecompta.getPeriodeCollection();
            for (Periode periodeCollectionOrphanCheckPeriode : periodeCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Exercicecompta (" + exercicecompta + ") cannot be destroyed since the Periode " + periodeCollectionOrphanCheckPeriode + " in its periodeCollection field has a non-nullable exercicecompta field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(exercicecompta);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Exercicecompta> findExercicecomptaEntities() {
        return findExercicecomptaEntities(true, -1, -1);
    }

    public List<Exercicecompta> findExercicecomptaEntities(int maxResults, int firstResult) {
        return findExercicecomptaEntities(false, maxResults, firstResult);
    }

    private List<Exercicecompta> findExercicecomptaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Exercicecompta.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Exercicecompta findExercicecompta(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Exercicecompta.class, id);
        } finally {
            em.close();
        }
    }

    public int getExercicecomptaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Exercicecompta> rt = cq.from(Exercicecompta.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
